package org.gdkm.el;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;

import org.gdkm.el.elt.AIEF;
import org.gdkm.el.elt.DataPack;

/**
 * 接入服务类. </br> 用于减低的程序的耦合性. </br> 日期: 2012-10-13
 * <p>
 * 
 * @author Arylo<<b>arylo.open@gmail.com</b>>
 * @version 1.0
 */
public class AccessServer implements Runnable {
	/**
	 * Socket 超时时间,当前为{@value} 秒
	 */
	public static final int second = 60;
	private static ServerSocket ss = null;

	/**
	 * 构建一个AccessServer对象.
	 */
	public AccessServer() {
		try {
			ss = new ServerSocket(Intent.getAccessPort());
			new Thread(this).start();
		} catch (IOException e) {
			System.err.println("ServerSocket init Fail.");
		}
		Intent.writeLog("Server", "The AccessServer Init finished.");
	}

	private void startAccess() {
		try {
			while (true) {
				Socket s = ss.accept();
				s.setSoTimeout(1000 * second);
				new Thread(new AccessServerThread(s)).start();
				Intent.writeLog("Server",
						"A User Connects To The Server.(" + s.getInetAddress()
								+ ":" + s.getPort() + ")");
			}
		} catch (IOException e) {
			System.err.println("Socket Build Fail.");
		}
	}

	/**
	 * Access线程.
	 */
	@Override
	public void run() {
		startAccess();
	}
}

/**
 * 接入服务线程类. </br> 便于程序的管理 </br> 日期: 2012-10-13
 * <p>
 * 
 * @author Arylo<<b>arylo.open@gmail.com</b>>
 * @version 1.0
 */
class AccessServerThread implements Runnable, AIEF {
	private static Socket s = null;
	private static ObjectInputStream oi = null;
	private static ObjectOutputStream oo = null;
	private static DataPack dp = null;
	private static String usr = null;

	/**
	 * 构建一个AccessServerThread对象.
	 * 
	 * @param s
	 *            所接入的IP的Socket
	 */
	public AccessServerThread(Socket s) {
		try {
			AccessServerThread.s = s;
			AccessServerThread.oo = new ObjectOutputStream(s.getOutputStream());
		} catch (IOException e) {
			System.err.println("Socket out init Fail.");
		}
		try {
			AccessServerThread.oi = new ObjectInputStream(s.getInputStream());
		} catch (IOException e) {
			System.err.println("Socket in init Fail.");
		}
	}

	private void writeLog(String text) {
		if (!usr.equals(null)) {
			Intent.writeLog(
					"Client",
					text + "(" + usr + ")(" + s.getInetAddress() + ":"
							+ s.getPort() + ")");
		}
	}

	/**
	 * AccessServerThread的接收线程.
	 */
	@Override
	public void run() {
		try {
			Thread.sleep(1000);
		} catch (Exception e) {
			e.printStackTrace();
		}
		try {
			while ((dp = (DataPack) oi.readObject()) != null) {
				if (dp.getIde().equals(SOCKET_AIEF)) {
					String text = dp.getText();
					if (text.equals(SOCKET_ONLINE)) {
						oo.writeObject(new DataPack(SOCKET_CONNECT));
					}
					if (text.equals(SOCKET_LOGIN)) {
						String accont = ((String) dp.getObject()).substring(0,
								((String) dp.getObject())
										.indexOf(SOCKET_LOGIN_SEP));
						String pwd = ((String) dp.getObject())
								.substring(((String) dp.getObject())
										.indexOf(SOCKET_LOGIN_SEP) + 1);
						String _s = Intent.checkAccout(accont, pwd);
						if (_s.equals(null)) {
							oo.writeObject(new DataPack(SOCKET_LFAIL));
						} else {
							usr = _s;
							oo.writeObject(new DataPack(SOCKET_LSUCCESS, usr));
							writeLog(SOCKET_LSUCCESS);
						}
					}
					if (text.equals(SOCKET_TREE_GET)) {
						oo.writeObject(new DataPack(SOCKET_TREE_SUCCESS, Intent
								.getFileList()));
						writeLog(SOCKET_TREE_GET);
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}